/**
 * @Author: 焦质晔
 * @Date: 2019/6/20
 * @Last Modified by: mikey.zhaopeng
 * @Last Modified time: 2019-12-21 21:39:26
 */
import { mapState } from 'vuex';
import { queryConsultant } from '@/api';

export const authority = {
  computed: {
    ...mapState('app', ['navList']),
    auths() {
      const target = this.deepFind(this.navList, this.$route.path) || {};
      return target.permission || [];
    },
    authsDisabled() {   // 根据权限判断按钮是否禁用
      const target = this.deepFind(this.navList, this.$route.path) || {};
      return !!target.permission;
    },
    getmenuId() {
      const target = this.deepFind(this.navList, this.$route.path) || {};
      return target.id || '';
    },
  },
  methods: {
    deepFind(arr, mark) {
      let res = null;
      for (let i = 0; i < arr.length; i++) {
        if (Array.isArray(arr[i].children)) {
          res = this.deepFind(arr[i].children, mark);
        }
        if (res !== null) {
          return res;
        }
        if (arr[i].key === mark) {
          res = arr[i];
          break;
        }
      }
      return res;
    },
    /*
    *isremoval:去重 不去重0 去重1
    *roleType: 职位code
    */
    async getConsultant(isremoval, roleType, menuId) {
      const data = {
        menuId:menuId || this.getmenuId,
        positionCode:roleType || localStorage.getItem("roleType") || ''
      }
      if(!data.positionCode || data.positionCode === 'null'){
        return []
      }
      const res = await queryConsultant(data);
      // console.log('销售顾问', res)
      if (res.resultCode === 200) {
        let arr = res.data.map(x => ({ value: x.userId, text: x.userName, orgId: x.orgId  }));
        if (isremoval) {
          var obj = {};
          arr = arr.reduce(function (item, next) {
            obj[next.value] ? '' : obj[next.value] = true && item.push(next);
            return item;
          }, [])
        }
        return arr;
      } else {
        return []
      }
    },
    authMark(url) {   // 根据权限判断按钮是否禁用
      const target = this.deepFind(this.navList, this.$route.path) || {};
      return !!target.permission && target.permission.includes(url);
    },
  }
};
